package com.yk.framework.gui.list;

import java.awt.BorderLayout;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.yk.framework.db.DBPool;
import com.yk.framework.gui.ctrl.YKTable;

/**
 * @author lifetime
 * 
 */
@SuppressWarnings("serial")
public class ListDataPanel extends JPanel {
	protected List<Map<String, Object>> listData;
	protected YKTable mainTable;
	protected TableColumnBeanManager columnBeanManager;
	protected String queryString;

	public ListDataPanel(String describeFile) {
		this.setLayout(new BorderLayout());
		rebuildTableData(describeFile);
		buildTable();
	}

	public void buildTable() {
		mainTable = new YKTable(new DefaultTableModel() {
			public int getRowCount() {
				return listData != null ? listData.size() : 0;
			}

			public int getColumnCount() {
				return columnBeanManager != null ? columnBeanManager.showSize() : 0;
			}

			public String getColumnName(int i) {
				return columnBeanManager != null ? columnBeanManager.getShowListElement(i).getHeadName() : "";
			}

			public boolean isCellEditable(int r, int c) {
				return false;
			}

			public Object getValueAt(int r, int c) {
				return listData != null ? listData.get(r).get(columnBeanManager.getShowListElement(c).getId()) : null;
			}

		});
		add(mainTable.getScrollPane(), BorderLayout.CENTER);
	}

	public void rebuildTableData(String uiDescFile) {
		InputStream stream = this.getClass().getClassLoader().getResourceAsStream(uiDescFile);
		if (uiDescFile == null) {
			stream = findDefaultFile();
		}
		if (stream == null) {
			throw new NullPointerException(" >>>>>>>>> not fond this ui describe file !");
		}
		SAXBuilder builder = new SAXBuilder();
		Document doc = null;
		try {
			doc = builder.build(stream);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				stream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (doc != null) {
			Element roote = doc.getRootElement();
			Element queryEle = roote.getChild("query");
			queryString = queryEle.getText();
			listData = DBPool.getInstance().executeQuery(queryString);
			Element tableEle = roote.getChild("table");
			columnBeanManager = new TableColumnBeanManager();
			List list = tableEle.getChildren("column");
			for (int i = 0, k = list.size(); i < k; i++) {
				Element e = (Element) list.get(i);
				TableColumnBean columnBean = new TableColumnBean();
				columnBean.setId(e.getAttribute("id").getValue());
				columnBean.setHeadName(e.getAttribute("headName").getValue());
				columnBean.setIsVisible(e.getAttribute("isVisible").getValue());
				columnBean.setQueryKey(e.getAttribute("queryKey").getValue() == null ? e.getAttribute("id").getValue() : e.getAttribute("queryKey").getValue());
				columnBeanManager.add(columnBean);
			}
		}
	}

	protected InputStream findDefaultFile() {
		String name = getClass().getName();
		name = name.replaceAll(".", "/");
		return this.getClass().getClassLoader().getResourceAsStream(name);
	}

	public void refresh() {
		listData = DBPool.getInstance().executeQuery(queryString);
		DefaultTableModel model = (DefaultTableModel) mainTable.getModel();
		model.fireTableDataChanged();
	}

	public List<Map<String, Object>> getListData() {
		return listData;
	}

	public void setListData(List<Map<String, Object>> listData) {
		this.listData = listData;
	}

	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public JTable getMainTable() {
		return mainTable;
	}

	public TableColumnBeanManager getColumnBeanManager() {
		return columnBeanManager;
	}

}
